<?php

/**
 * uniBoard User 2 User E-Mail form
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_email{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * User API
	 *
	 * @var object
	 */
	
	private $api 			= NULL;
	
	/**
	 * Email API
	 *
	 * @var object
	 */
	
	private $email_api		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Load Language File
		$appCore -> loadLanguageFile('users');
		
		// Try
		try
		{
			// Are we logged in?
			if ( $appCore -> user['id'] < 1)
			{
				throw new exc_small($appCore -> lang['action_not_allowed_guest']);
			}
			
			// Are we going to send e-mail to ourself?
			if ( $appCore -> user['id'] == $appCore -> _REQ('user'))
			{
				throw new exc_small($appCore -> lang['user_email_cant_yourself']);
			}
			
			// No E-Mail access?
			if ( $appCore -> user_acl['email_access'] == 0)
			{
				throw new exc_small($appCore -> lang['action_not_allowed_access']);
			}
			
			// Target User ID incorrect?
			if ( $appCore -> _REQ('user') < 1)
			{
				throw new exc_small($appCore -> lang['api_user_not_found']);
			}
			
			// Start API
			$this -> api = new api_user($appCore, $appCore -> _REQ('user'));
			
			// User not found
			if ( $this -> api -> error)
			{
				throw new exc_small($appCore -> lang['api_user_not_found']);
			}
			
			// User not found
			if ( !$this -> api -> user_want_emails)
			{
				throw new exc_small(sprintf($appCore -> lang['user_email_do_not_want'], htmlspecialchars($this -> api -> user_name)));
			}
			
			// Email sent?
			if ($appCore -> _REQ('done') == 1)
			{
				// Set Event
				$appCore -> output -> setEvent(sprintf($appCore -> lang['user_email_sent'], htmlspecialchars($this -> api -> user_name)), $appCore -> lang['user_send_email']);
				
				// Draw Page
				$appCore -> output -> drawEvents(true, false);
			}
			else
			{
				// Start API
				$this -> email_api = new api_user_email($appCore);
				
				// Fill it with basic stuff
				$this -> email_api -> setUserEmailName($this -> api -> user_name);
				$this -> email_api -> setUserEmailAddress($this -> api -> user_email);
				
				// Send Message
				if ( $appCore -> _POST('update') == 1)
				{
					// Fill api
					$this -> email_api -> setUserEmailSubject($appCore -> _POST('email_subject'));
					$this -> email_api -> setUserEmailMessage($appCore -> _POST('email_message'));
					
					// Check all teh stuff
					try
					{
						// Request Valid?
						if (!$appCore -> validRequest())
						{
							throw new exc_small($appCore -> lang['wrong_action_auth'], $appCore -> lang['wrong_action_auth_title']);
						}
						
						// Api has errors?
						if ($this -> email_api -> error)
						{
							throw new exc_small(join("<br/>", $this -> email_api -> errors), $appCore -> lang['user_send_email']);
						}
						
						// Send E-Mail
						$this -> email_api -> sendUserEmail($this -> api -> user_language);
						
						// Redirect us
						$appCore -> setRedirect($appCore -> niceLink('act=email&user=' . $this -> api -> user_id . '&done=1', $this -> api -> user_name), false);
					}
					catch (exc_small $exception)
					{
						// Set Error
						$appCore -> output -> setEvent( $exception -> message, $exception -> title, 0);
						
						// Repeat Form
						$this -> drawForm();
					}
				}
				else
				{
					// Draw Form
					$this -> drawForm();	
				}
			}
		}
		catch (exc_small $exception)
		{
			// Set Error
			$appCore -> output -> setEvent( $exception -> message, $appCore -> lang['user_send_email'], 0);
			
			// Draw Events
			$appCore -> output -> drawEvents();
		}
		
	}
	
	function drawForm(){
		
		// Load Templates
		$templates = new tpl_user_profile( $this -> appCore);
						
		// Main template vars
		$tpl_vars = array(
			'act_code'			=> $this -> appCore -> session -> session_code,
			'act_key'			=> $this -> appCore -> session -> session_key,
			'user_id' 			=> $this -> api -> user_id,
			'user_name'			=> $this -> api -> user_name,
			'email_subject'		=> $this -> email_api -> user_email_subject,
			'email_message'		=> $this -> email_api -> user_email_message,
		);
						
		// Draw Page
		$this -> appCore -> output -> draw( $this -> appCore -> lang['user_send_email'], array(
			array(
				$this -> appCore -> niceLink('act=users', $this -> appCore -> lang['board_nav_users']),
				$this -> appCore -> lang['board_nav_users']
			),
			array(
				$this -> appCore -> niceLink('act=email&user=' . $this -> api -> user_id, $this -> api -> user_name),
				$this -> appCore -> lang['user_send_email_short']
			)
		), $templates -> tpl_email_body( $tpl_vars));		
		
	}
	
}